package com.zyf.ppx.adapter

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.example.zzh.foldtext.SpannableFoldTextView
import com.jaeger.ninegridimageview.NineGridImageView
import com.jaeger.ninegridimageview.NineGridImageViewAdapter
import com.zyf.ppx.R
import com.zyf.ppx.bean.Comment
import com.zyf.ppx.bean.MuitImage
import com.zyf.ppx.ui.ImageActivity
import com.zyf.ppx.utils.DisplayUtils

class CommentAdapter(data:List<Comment>) :BaseQuickAdapter<Comment,BaseViewHolder>(R.layout.comment_item,data){
    var width = 0
    var height  = 0
    var options:RequestOptions = RequestOptions()
        .centerCrop()
        //.placeholder(R.mipmap.ic_launcher_round)
        .error(android.R.drawable.stat_notify_error)
        .priority(Priority.HIGH)
        .fitCenter()
        //.skipMemoryCache(true)
        .diskCacheStrategy(DiskCacheStrategy.DATA)
    override fun convert(helper: BaseViewHolder?, item: Comment?) {
        helper?.setText(R.id.nick,item?.comment_info?.user?.name)
        Glide.with(mContext).load("https://p3-ppx.bytecdn.cn/img/"+item?.comment_info?.user?.avatar?.uri+"~200x200.webp").apply(RequestOptions.bitmapTransform(
            CircleCrop()
        )).into(helper?.getView(R.id.header_image)!!)
        helper?.setText(R.id.god_fabulous,item?.comment_info?.user?.like_count.toString())
        if(TextUtils.isEmpty(item?.comment_info?.text)){
            helper?.getView<SpannableFoldTextView>(R.id.content)?.visibility= View.GONE
        }else{
            helper?.getView<SpannableFoldTextView>(R.id.content)?.visibility= View.VISIBLE
        }
        helper?.getView<SpannableFoldTextView>(R.id.content)?.text = item?.comment_info?.text
        var gridlayout = helper?.getView<NineGridImageView<String>>(R.id.iv_ngrid_layout)
        var gifView =  helper?.getView<ImageView>(R.id.gifView)
        var gifIcon =  helper?.getView<ImageView>(R.id.isGif)
        if(item?.comment_info?.images!=null){
            gridlayout?.visibility =View.GONE
            gifView?.visibility =View.VISIBLE
            gifIcon?.visibility =View.VISIBLE
            val windowManager = mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
            width = windowManager.defaultDisplay.width
            height = windowManager.defaultDisplay.height
            var imageUrl = ArrayList<String>()
            item.comment_info?.images?.forEach{
                imageUrl.add(it.url_list[0].url)
            }
            if(imageUrl.size==1){
                var multi = item?.comment_info?.images!![0]
                var params: RelativeLayout.LayoutParams = gifView?.layoutParams as RelativeLayout.LayoutParams
                params.leftMargin = 0
                DisplayUtils.dp2px(mContext,16.0f)
                if(multi.height<multi.width){
                    params.width = width
                    params.height = (((width/(multi.width*1.0f))*multi.height).toInt())
                }else{
                    if(multi.width<width){
                        params.width = multi.width
                        params.height =  multi.height
                        if(multi.width<width/2){
                            params.width = (width*0.4f).toInt()
                            params.height =  (multi.height*((width*0.4f)/multi.width)).toInt()
                        }
                        params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                    }else{
                        params.width = width
                        params.height = (multi.width/width)*multi.height
                        Log.e("比例",(multi.height/(multi.width*1.0f)).toString())
                        if((multi.height/(multi.width*1.0f)) > 1.7f){
                            params.width = (width*0.4f).toInt()
                            params.height = (0.4f*multi.height).toInt()
                            params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                        }
                        if((multi.height/(multi.width*1.0f)) > 1.3f){
                            params.width = (width*0.4f).toInt()
                            params.height = (0.4f*multi.height).toInt()
                            params.leftMargin = DisplayUtils.dp2px(mContext,16.0f)
                        }
                    }
                }

                Log.e("宽高","params.width"+params.width+":params.height"+params.height)
                gifView.layoutParams = params

                if(multi.is_gif){
                    gifIcon?.visibility = View.VISIBLE

                }else{
                    gifIcon?.visibility = View.GONE
                }
                if (gifView != null) {
                    Glide.with(mContext).load(multi.download_list[0].url).apply(options).thumbnail(0.1f).into(gifView)
                }
                gifView.setOnClickListener {
                    var intent = Intent(mContext, ImageActivity::class.java)
                    intent.putExtra("pos",0)
                    var imageData = ArrayList<MuitImage>()
                    imageData.add(multi)
                    intent.putExtra("data",imageData)
                    mContext.startActivity(intent)
                }
                gifView?.visibility = View.VISIBLE
                gridlayout?.visibility = View.GONE
            }else{
                gifView?.visibility = View.GONE
                gifIcon?.visibility =View.GONE
                gridlayout?.visibility = View.VISIBLE
                gridlayout?.setAdapter(object : NineGridImageViewAdapter<String>(){
                    override fun onDisplayImage(context: Context?, imageView: ImageView?, t: String?) {
                        Glide.with(context!!).load(t).thumbnail(0.1f).into(imageView!!)
                    }

                    override fun onItemImageClick(context: Context?, index: Int, list: MutableList<String>?) {
                        var intent = Intent(mContext, ImageActivity::class.java)
                        intent.putExtra("pos",index)
                        intent.putExtra("data",item?.comment_info?.images as ArrayList<MuitImage>)
                        mContext.startActivity(intent)
                    }
                })
                gridlayout?.setImagesData(imageUrl)
            }
        }else{
            gridlayout?.visibility =View.GONE
            gifView?.visibility =View.GONE
            gifIcon?.visibility =View.GONE
        }
    }
}